package com.aiserver.controller;

import com.aiserver.common.Result;
import com.aiserver.service.SmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * 短信服务测试控制器
 * 仅用于开发和测试环境
 */
@RestController
@RequestMapping("/sms")
public class SmsController {

    private static final Logger logger = LoggerFactory.getLogger(SmsController.class);

    @Autowired
    private SmsService smsService;

    /**
     * 测试发送短信验证码
     */
    @PostMapping("/test-code")
    public Result<String> testSendCode(
            @NotBlank(message = "手机号不能为空") @RequestParam String phone,
            @NotBlank(message = "验证码不能为空") @RequestParam String code) {
        try {
            boolean success = smsService.sendCode(phone, code);
            if (success) {
                return Result.success("短信发送成功", "验证码已发送到 " + phone);
            } else {
                return Result.error("短信发送失败");
            }
        } catch (Exception e) {
            logger.error("测试发送短信失败", e);
            return Result.error("短信发送异常: " + e.getMessage());
        }
    }

    /**
     * 测试发送自定义短信
     */
    @PostMapping("/test-custom")
    public Result<String> testSendCustomSms(
            @NotBlank(message = "手机号不能为空") @RequestParam String phone,
            @NotBlank(message = "模板ID不能为空") @RequestParam String templateId,
            @RequestParam(required = false) String[] params) {
        try {
            if (params == null) {
                params = new String[0];
            }
            boolean success = smsService.sendSms(phone, templateId, params);
            if (success) {
                return Result.success("短信发送成功", "自定义短信已发送到 " + phone);
            } else {
                return Result.error("短信发送失败");
            }
        } catch (Exception e) {
            logger.error("测试发送自定义短信失败", e);
            return Result.error("短信发送异常: " + e.getMessage());
        }
    }
}